#include "MainWindow.h"
#include <amk3/EarthWrapper.h>
#include <QQmlContext>
#include <QVBoxLayout>
#include "QMLInterface.h"
#include "AppConfig.h"
#include "MainService.h"
#include "SysConfig.h"

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
	ui.setupUi(this);

	EarthWrapper::instance()->createMapNode();

	m_earthview = new Earth3DView();
	m_earthview->rootContext()->setContextProperty("mainApp", this);
	//m_earthview->rootContext()->setContextProperty("earthView", m_earthview);
    m_earthview->rootContext()->setContextProperty("app", QMLInterface::instance());
	m_earthview->setSource(QUrl::fromLocalFile("../../src/qml/EarthMain.qml"));
	m_earthview->setResizeMode(QQuickView::SizeRootObjectToView);
	m_earthWidget = QWidget::createWindowContainer(m_earthview);

	QVBoxLayout *center_layout = new QVBoxLayout();
	center_layout->addWidget(m_earthWidget);
	center_layout->setSpacing(0);
	center_layout->setContentsMargins(0, 0, 0, 0);
	ui.centralWidget->setLayout(center_layout);

    m_myWebView = new MyWebEngineView(this);
    connect(m_myWebView, SIGNAL(backMy3D()), this, SLOT(slotShowEarthWindow()));
    center_layout->addWidget(m_myWebView);


	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_index)));
	m_myWebView->show();
	m_earthWidget->hide();

	connect(ui.actionSearch, SIGNAL(triggered()), this, SLOT(slotSearch()));

	connect(ui.action3D, SIGNAL(triggered()), this, SLOT(slotShowEarthWindow()));
	connect(ui.actionAttack, SIGNAL(triggered()), this, SLOT(slotShowAttack()));
	connect(ui.actionTopology, SIGNAL(triggered()), this, SLOT(slotShowVisTopology()));

	connect(ui.actionAttack2, SIGNAL(triggered()), this, SLOT(slotShowVisAttack2()));
	connect(ui.actionVisSent, SIGNAL(triggered()), this, SLOT(slotShowVisSent()));

	connect(ui.actionSocial, SIGNAL(triggered()), this, SLOT(slotShowSocial()));
	
	connect(ui.actionSM_IPLoc, SIGNAL(triggered()), this, SLOT(slotSMIpLoc()));
	connect(ui.actionSM_IPTrack, SIGNAL(triggered()), this, SLOT(slotIPTrack()));
	connect(ui.actionSM_Port, SIGNAL(triggered()), this, SLOT(slotSMPort()));
	connect(ui.actionSM_Topo, SIGNAL(triggered()), this, SLOT(slotSMTopology()));
	connect(ui.actionSM_Monitor, SIGNAL(triggered()), this, SLOT(slotSMMonitor()));
	

	connect(ui.actionRES_Landmarks, SIGNAL(triggered()), this, SLOT(slotShowLandmarks()));
	connect(ui.actionRES_OpticalCable, SIGNAL(triggered()), this, SLOT(slotShowOpticalCable()));


	//connect(ui.ac, SIGNAL(triggered()), this, SLOT(slotIPTrack()));
}

MainWindow::~MainWindow()
{
	
}

void MainWindow::slotShowEarthWindow()
{
    m_myWebView->hide();
    m_earthWidget->showMaximized();

}

void MainWindow::slotShowDatabaseView()
{
	m_myWebView->load(QUrl("http://116.255.255.137:8088/datamanager/databaseManager/databaseManager.html"));
    //m_myWebView->load(QUrl(AppConfig::instance()->SERVER_PATH + "/datamanager/databaseManager/databaseManager.html"));
    m_earthWidget->hide();
    m_myWebView->showMaximized();
}

void MainWindow::slotShowWebViewUrl(QString url)
{
    if (url.startsWith('/')) {
        url = AppConfig::instance()->SERVER_PATH + url;
    }
    qDebug() << url;
    m_myWebView->load(QUrl(url));
    m_earthWidget->hide();
    m_myWebView->showMaximized();
}

void MainWindow::slotShowWebView()
{
	if (!m_myWebView){
		m_myWebView = new MyWebEngineView(this);
		//connect(m_myWebView, SIGNAL(backMy3D()), this, SLOT(slotShowEarthWindow()));
		m_myWebView->hide();
		ui.centralWidget->layout()->addWidget(m_myWebView);
	}
	m_myWebView->load(QUrl(AppConfig::instance()->SERVER_PATH + "/xeye/cybermap/ipquery.html"));
	m_earthWidget->hide();
	m_myWebView->showMaximized();
	//m_myWebView->setZoomFactor(1.5);
	return;
}

void MainWindow::slotShowAttack()
{
	m_earthWidget->showMaximized();
	m_myWebView->hide();
	MainService::instance()->showCyberThreat();
}

void MainWindow::slotShowVisTopology()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_vistopo)));
	//MainService::instance()->showCyberThreat();
}

void MainWindow::slotShowVisAttack2()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();

	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_visattack)));
	//MainService::instance()->showCyberThreat();
}

void MainWindow::slotShowVisSent()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();

	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_vissent)));
}

void MainWindow::slotShowSocial()
{

	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_social)));

	//slotIPTrack();
}

void MainWindow::slotIPTrack()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	QString url = QString::fromStdString(SysConfig::intance()->url_iptrack);
	m_myWebView->load(QUrl(url));
}

void MainWindow::slotShowLandmarks()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_landmark)));
}

void MainWindow::slotShowOpticalCable()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_optical)));
}

void MainWindow::slotSMTopology()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_smt)));
}

void MainWindow::slotSMIpLoc()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_iploc)));
}

void MainWindow::slotSMPort()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_port)));
}

void MainWindow::slotSearch()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl(QString::fromStdString(SysConfig::intance()->url_search)));
}

void MainWindow::slotSMMonitor()
{
	m_myWebView->showMaximized();
	m_earthWidget->hide();
	m_myWebView->load(QUrl("http://wave.ipplus360.com/"));
}